package com.ing5.BL.android.cinece;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.Toast;

import com.ing5.BL.android.cinece.api.client.GetSearch;
import com.ing5.BL.android.cinece.api.model.SearchResults;
import com.ing5.BL.android.cinece.util.CacheClient;

public class MainActivity extends Activity {
	
	private SearchView searchView;
	private ListView listView;
	private ProgressBar progBarSearch;
	private MovieListAdapter movieAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		listView = (ListView) findViewById(R.id.listView);
		progBarSearch = (ProgressBar) findViewById(R.id.progBarSearch);
		
		progBarSearch.setVisibility(View.GONE);
		listView.setVisibility(View.GONE);
		
		searchView = (SearchView) findViewById(R.id.search);		
		searchView.setOnQueryTextListener(new OnQueryTextListener() {
			
			@Override
			public boolean onQueryTextSubmit(String query) {
				if(query.length() != 0) {
					searchView.clearFocus();
					new GetResultTask().execute(new GetSearch(query));
					return true;
				}
				return false;
			}
			
			@Override
			public boolean onQueryTextChange(String newText) {
				return false;
			}
		});
	}
	
	class GetResultTask extends AsyncTask<GetSearch, SearchResults, SearchResults> {

		@Override
		protected void onPreExecute() {
			listView.setVisibility(View.GONE);
			progBarSearch.setVisibility(View.VISIBLE);
			progBarSearch.bringToFront();
			super.onPreExecute();
		}
        @Override
        protected SearchResults doInBackground(GetSearch... getSearch) {
            SearchResults results = null;

            try {
             results = new CacheClient(getApplicationContext()).load(getSearch[0]);
            } catch (Exception e) {
            	Log.e("DEBUG", "AsyncTask - Load Failed");
            }            
            return results;
        }

        @Override
        protected void onPostExecute(SearchResults results) {
            // if the user is still in the view
            if (results != null) { //blindage
            	progBarSearch.setVisibility(View.GONE);
            	listView.setVisibility(View.VISIBLE);    			
            	if(!results.getResultList().isEmpty()) adapteList(results);
            }
            else { 
            	progBarSearch.setVisibility(View.GONE);
            	listView.setVisibility(View.GONE);  
            	noResultToast();
            }
        }
    }
	
	public void adapteList(SearchResults results) {
		movieAdapter = new MovieListAdapter(this, results.getResultList());
		listView.setAdapter(movieAdapter);
	}
	
	public void noResultToast() {
		Toast.makeText(this, "No Result Found", Toast.LENGTH_LONG).show();;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
